package com.vogue.supplymall.order.domain.dao.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;

import com.vogue.ssm.database.util.DealStatus;
import com.vogue.supplymall.order.domain.dao.OrderPullDao;
import com.vogue.supplymall.order.domain.mapper.OrderPullMapper;
import com.vogue.supplymall.order.domain.model.OrderPull;

@Repository("orderPullDao")
public class OrderPullDaoImpl extends BaseDaoImpl<OrderPull, Long> implements OrderPullDao {

	@Resource
    private OrderPullMapper orderPullMapper;
	@Override
	public List<Map<String, Object>> pullSell(Long syscodeid, Date maxdate) {
		DealStatus.kacha();
		return orderPullMapper.pullSell(syscodeid, maxdate);
	}

	@Override
	public List<Map<String, Object>> pullBuy(Long syscodeid, Date maxdate) {
		return orderPullMapper.pullBuy(syscodeid, maxdate);
	}

	@Override
	public List<Map<String, Object>> findOrdernumbers(Long syscodeId, String orderNums, String repeatIds) {
		String sql = "select ordernumber"
				+ " from `order`"
				+ " where syscodeid=" + syscodeId + " and"
				+ " ordernumber in (" + orderNums + ") and ordernumber not in (" + repeatIds + ")";
		List<Map<String, Object>> list = this.query(sql);
		return list;
		//return orderPullMapper.findOrdernumbers(syscodeId, orderNums,repeatIds);
	}

	@Override
	public String updateOrderBillStatus() {
		String sql = "update order_status os, `order` o, order_seller ose "
		+ " set os.orderstatus = #{status}, os.modifydate = now(),os.pullstate=#{pullstate} "
		+ " where os.orderid = o.id and os.orderid = ose.orderid "
		+ " and os.iscancel = 0 and os.pullstate = 1 "
		+ " and ose.syscodeid =#{syscodeId} " 
		+ " and o.ordernumber in (#{orderNums})"
		+ " and os.orderstatus<>0 "
		+ " and os.orderstatus<>4 ";
		return sql;
	}

	@Override
	public String updateBuyOrderBillStatus() {
		String sql = "update order_status os, `order` o, order_buyer ose "
				+ " set os.orderstatus = #{status}, os.modifydate = now(),os.buypullstate=#{buypullstate} "
				+ " where os.orderid = o.id and os.orderid = ose.orderid "
				+ " and os.iscancel = 0 and os.buypullstate = 1 "
                + " and ose.syscodeid =#{syscodeId} "
				+ " and o.ordernumber in (#{orderNums})"
				+ " and os.orderstatus<>0 "
				+ " and os.orderstatus>2 ";
				return sql;
	}

	@Override
	public String findUpdateFailBill() {
		String sql = "select o.ordernumber,os.iscancel, CONVERT(os.orderstatus,SIGNED) orderstatus"
				+ "	from `order` o,order_status os"
				+ " where o.syscodeid=#{syscodeId}"
				+ " and o.ordernumber in (#{orderNums})"
				+ " and o.id=os.orderid and os.orderstatus<>#{status}";
		return sql;
	}

	@Override
	public String findOrderNumsByOrderNums() {
		String sql = "select o.ordernumber"
				+ " from `order` o"
				+ " where o.ordernumber in (#{orderNums})";
		return sql;
	}

	@Override
	public Map<String, Object> pullSellCount(Long syscodeid, Date maxdate) {
		return orderPullMapper.pullSellCount(syscodeid, maxdate);
	}

	@Override
	public Map<String, Object> pullBuyCount(Long syscodeid, Date maxdate) {
		return orderPullMapper.pullBuyCount(syscodeid, maxdate);
	}
	@Override
	public List<Map<String, Object>> getPayModeByOrderId(String orderIds) {
		String sql = "select op.paymenttype*1 paymenttype,op.paymentmode*1 paymentmode,op.amount,o.ordernumber "
				+ " from order_payment op,`order` o "
				+ " where op.orderid in (" + orderIds + ") and op.orderid=o.id";
		return this.query(sql);
	}

	@Override
	public Map<String, Object> pullRefundSellCount(Long syscodeid, Date maxdate) {
		return orderPullMapper.pullRefundSellCount(syscodeid, maxdate);
	}

    @Override
    public List<Map<String, Object>> pullRefundSell(Long syscodeid, Date maxdate) {
        DealStatus.kacha();
        return orderPullMapper.pullRefundSell(syscodeid, maxdate);
    }

	@Override
	public List<Map<String, Object>> getPayModeByRefundOrderId(String orderIds) {
		String sql = "select p.paymenttype*1 paymenttype,p.paymentmode*1 paymentmode,p.amount,r.refundnumber " +
					"from refund_payment p,refund_order r " +
					"where r.id in (" + orderIds + ") and p.refundid = r.id";

		return this.query(sql);
	}

	@Override
	public String updateRefundBillStatus() {
		String sql = "update refund_status os, refund_order o, order_seller ose "
				+ " set os.refundstatus = #{status}, os.modifydate = now(),os.sellerpullstate=#{pullstate} "
				+ " where os.refundid = o.id and o.orderid = ose.orderid "
				+ " and os.sellerpullstate = 1 "
				+ " and ose.syscodeid =#{syscodeId} "
				+ " and o.refundnumber in (#{orderNums})"
				//+ " and os.refundstatus=0 "
				+ " and os.refundstatus<>3 ";
		return sql;
	}

	@Override
	public String findUpdateFailRefundBill() {
		String sql = "select o.refundnumber ordernumber, CONVERT(os.refundstatus,SIGNED) orderstatus "
				+ "	from refund_order o,refund_status os"
				+ " where o.syscodeid=#{syscodeId}"
				+ " and o.refundnumber in (#{orderNums})"
				+ " and o.id=os.refundid "
				+ " and os.refundstatus<>#{status}";
		return sql;
	}
}
